package cn.life.business.user.service.impl;

import cn.life.business.user.entity.vo.LoginVo;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.utils.EncryptPwdUtil;
import cn.life.business.user.dao.UserDao;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.business.user.service.UserService;
import cn.life.common.utils.SM.SM2Utils;
import cn.life.common.utils.SM.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:    用户服务实现层
 * @Author 李佳明
 * @Date 2018/7/5 0005.6:56.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService{

    private String priKey = "9884F11A387C15EDA2FDA16DB93925A86F9C2E4657C2D7FE241A57F379C9144E";

    @Autowired
    private UserDao userDao;

    @Override
    public CMessage keyLogin(HttpServletRequest request, HttpServletResponse response, LoginVo loginVo){
        //1.认证用户加密iKey信息
        String decText = null;
        try {
            decText = new String(SM2Utils.decrypt(Util.hexToByte(priKey), Util.hexToByte(loginVo.getEncCode()))).trim();
        } catch (IOException e) {
            e.printStackTrace();
            return CMessage.fail("用户认证失败");
        }
        //2.认证用户密码
        if(decText != null && loginVo.getCode().equals(decText)){
            UserInfo userInfo = userDao.queryUserInfo(loginVo);
            if(userInfo != null){
                //3.创建session
                //用户访问过之后重新设置用户的访问时间，存储到cookie中，然后发送到客户端浏览器
                String cookieValue = userInfo.getUserId() + "=" + userInfo.getState();
                Cookie cookie = new Cookie("factoryCookie",cookieValue);//创建一个cookie，cookie的名字是lastAccessTime
                cookie.setPath("/");
                //将cookie对象添加到response对象中，这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                response.addCookie(cookie);
                //增加session
                MyUserInfo myUserInfo = new MyUserInfo();
                myUserInfo.setUserId(userInfo.getUserId());
                myUserInfo.setUserName(userInfo.getUserName());
                myUserInfo.setState(userInfo.getState());
                String sessionKey = userInfo.getUserId() + "="  + userInfo.getState();
                request.getSession().setAttribute(sessionKey,myUserInfo);

                return CMessage.success("登陆成功",userInfo);
            }
            return CMessage.fail("用户密码错误，请输入正确密码。");
        }
        return CMessage.fail("用户认证失败");
    }

    @Override
    public CMessage adminLogin(HttpServletRequest request, HttpServletResponse response, LoginVo loginVo) {
        UserInfo userInfo = userDao.queryUserInfo(loginVo);
        if(userInfo != null) {
            //3.创建session
            //用户访问过之后重新设置用户的访问时间，存储到cookie中，然后发送到客户端浏览器
            String cookieValue = userInfo.getUserId() + "=" + userInfo.getState();
            Cookie cookie = new Cookie("factoryCookie", cookieValue);//创建一个cookie，cookie的名字是lastAccessTime
            cookie.setPath("/");
            //将cookie对象添加到response对象中，这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            response.addCookie(cookie);
            //增加session
            MyUserInfo myUserInfo = new MyUserInfo();
            myUserInfo.setUserId(userInfo.getUserId());
            myUserInfo.setUserName(userInfo.getUserName());
            myUserInfo.setState(userInfo.getState());
            String sessionKey = userInfo.getUserId() + "=" + userInfo.getState();
            request.getSession().setAttribute(sessionKey, myUserInfo);

            return CMessage.success("登陆成功", userInfo);
        }
        return CMessage.fail("用户密码错误，请输入正确密码。");
    }
}
